﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Production.ApplicationLib.Command;
using Production.ApplicationLib.Queries;
using Production.CommonLib.Constants;
using Production.CommonLib.Models;

namespace Production.WebApi.Controllers
{
    [ApiController]
    public class ProductionPlanController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IProductionPlanQueries _productionPlanQueries;

        public ProductionPlanController(IMediator mediator,
            IProductionPlanQueries productionPlanQueries)
        {
            _mediator = mediator;
            _productionPlanQueries = productionPlanQueries;
        }


        [HttpGet(ApiRoutes.ProductionPlan.QueryProductionPlans)]
        public async Task<IActionResult> Get([FromQuery] ProductionPlanSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _productionPlanQueries.QueryProductionPlansByPage(model);
                return Ok(data);
            }
            else
            {
                var data = await _productionPlanQueries.QueryProductionPlans(model);
                return Ok(data);
            }
        }


        [HttpPost(ApiRoutes.ProductionPlan.CreateProductionPlan)]
        public async Task<IActionResult> Post(CreateProductionPlanCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpPost(ApiRoutes.ProductionPlan.CalculatingProductionPlan)]
        public async Task<IActionResult> Calculating(CalculatingProductionPlanCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

    }
}